package com.manage.base.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.manage.base.util.ConfigKeys;
import com.manage.base.util.SystemPropertiesUtils;
import com.manage.util.StringUtils;

/**
 * 
 * @Description: 拦截器补充一些属性东西
 * 
 *               用于新功能的一些基本信息设置
 * 
 * @author weisd Email:xiyangdewuse@163.com
 * @date 2011-10-12
 * @version v1.0
 */
public class BaseN1Interceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
		String contentType = res.getContentType();
		if (StringUtils.isEmptyOrNullByTrim(contentType)) {
			res.setContentType("text/html;charset=UTF-8");
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse res, Object handler, ModelAndView modelAndView) throws Exception {
		String ctxPath = req.getContextPath();
		String encoding = SystemPropertiesUtils.globals.getString(ConfigKeys.ENCODING);
		String templateName = SystemPropertiesUtils.globals.getString(ConfigKeys.TEMPLATENAME);
		String extension = SystemPropertiesUtils.globals.getString(ConfigKeys.SERVLET_EXTENSION);
		if (null != modelAndView) {
			String viewName = modelAndView.getViewName();
			if(null == viewName || !viewName.startsWith("redirect:")){
				modelAndView.addObject("extension", extension);
				modelAndView.addObject("encoding", encoding);
				modelAndView.addObject("templateName", templateName);
				modelAndView.addObject("ctxPath", ctxPath);
			}
		}
	}

}
